import { useState, useEffect } from 'react';
import { getScocketUrl } from '~/api/loginApi';

export const useSocketUrl = () => {
    const [socketUrl, setSocketUrl] = useState('');
    useEffect(() => {
        const fetchSocketUrl = async () => {
            try {
                const res = await getScocketUrl();
                const { port, url } = res;
                const socketUrl = `wss://${url}:${port}/mqtt`;
                setSocketUrl(socketUrl);
            } catch (error) {
                console.error('Error fetching socket URL:', error);
            }
        };
        fetchSocketUrl();
    }, []);
    return socketUrl;
}